home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / modlib_s.lha / modlib_src / $init_sys.P < prev    next >
Text File  |  1990-04-12  |  8KB  |  234 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $init_sys.P */
  25.  
  26. /* These are the module handling routines. The main routine is the
  27.    undefined_pred interrupt handler. The system keeps a table of modules
  28.    and routines that are needed from each. When a predicate is found to
  29.    be undefined, the table is searched to see if it is defined by 
  30.    some module. If so, that module is loaded (if it hasn't been previously
  31.    loaded) and the association is made between the routine name as 
  32.    defined in the module, and the routine name as used by the invoker.
  33.  
  34.    The table of modules and needed routines is:
  35.    defined_mods(modname,[pred1/arity1,...,predn/arityn]).
  36.      where modname is the name of the module. The module exports n
  37.     predicate definitions. The first exported pred is of arity 
  38.     arity1, and needs to be invoked by the name of pred1.
  39.  
  40.    The table of modules that have already been loaded is:
  41.    loaded_mods(modname).
  42.  
  43.    A module is a file of predicate definitions. Consider a module name
  44.    `mod'. It contains a single fact, named mod_export, that is true of
  45.    the list of predicate/arity's that are exported. 
  46.    e.g. mod_export([mod_p1/2,mod_p2/4]).
  47.    For each module, m, which contains predicates needed by this module,
  48.    there is a mod_use fact, describing what module is needed and the names
  49.    of the predicates defined there that are needed. For example, if module
  50.    mod needs to import predicates from mod m, there would be a fact:
  51.    mod_use(m,[mod_ip1/2,mod_ip2/4]), where m is a module that exports two
  52.    predicates: one 2-ary and one 4-ary. This list corresponds to the export
  53.    list of module m.
  54.  
  55.    The routines $load_mod/1 and $define_mod/2 are exported.
  56. */
  57. '$init_sys_export'([$define_mod/2,$load/1,$load/2,$load_mod/1]).
  58.  
  59. inform(X) :- '_$builtin'(133), '_$builtin'(26).
  60.  
  61. '$init_sys' :-
  62.         inform('init_sys !' ), 
  63.     inform('loading: assert'), $load($assert),
  64.     inform('loading: db    '), $load($db),
  65.     inform('loading: dbcmpl'), $load($dbcmpl),
  66.     inform('loading: bio   '), $load($bio),
  67.     inform('loading: bmeta '), $load($bmeta),
  68.     $load($name),
  69.     $load($blist),
  70.     $load($buff),
  71.     $load($call),
  72.     $load($glob),
  73.     $assert_union(loaded_mods(_),$loaded_mods(_)), /* nec for compile */
  74.     $assert_abolish_i(defined_mods(_,_)),    /* nec for compile [a] */
  75.     $globalset('_$nofile_msg'(1)),
  76.     $globalset('_$cputime'(0)).
  77.  
  78. /* for tracing while debugging this module */
  79. $load_writename(X) :- $load_wn(X),$load_put(10).
  80. $load_wn(X) :- '_$builtin'(133). /* writename */
  81. $load_put(X) :- '_$builtin'(24). /* put */
  82.  
  83. /* defined_mods(_,_) :- fail. */
  84.  
  85. $define_mod(Mod,Implist) :- defined_mods(Mod,Implist),!. /* no-op if there */
  86. $define_mod(Mod,Implist) :- $assert(defined_mods(Mod,Implist)).
  87.  
  88. $loaded_mods($init_sys).
  89. $loaded_mods($assert).
  90. $loaded_mods($db).
  91. $loaded_mods($dbcmpl).
  92. $loaded_mods($bio).
  93. $loaded_mods($bmeta).
  94. $loaded_mods($name).
  95. $loaded_mods($blist).
  96. $loaded_mods($buff).
  97. $loaded_mods($call).
  98. $loaded_mods($glob).
  99.  
  100. /* the undefined_pred interrupt handler: */
  101.  
  102.  
  103. '_$undefined_pred'(Term) :-
  104.     $functor0(Term,Pred),
  105.     $arity(Term,Arity),
  106.     ('_$nodynload'(Pred,Arity) ->
  107.         fail ;
  108.         (not(not('_$definable'(Term))),'_$call'(Term))
  109.     ).
  110.  
  111. '_$nodynload'(_,_) :- fail.
  112.  
  113. /* **********
  114. '_$undefined_pred'(Term) :-
  115.     not(not('_$definable'(Term))),'_$call'(Term).
  116. ********** */
  117.  
  118. '_$definable'(Term) :- 
  119.     $functor0(Term,Pred),$arity(Term,Arity),
  120. /*      $telling(F),$tell(user),$writename('loading: '),$writename(Pred),
  121.       $writename('/'),$writename(Arity),$nl,$tell(F), */
  122.     defined_mods(Module_name,Pred_name_list),  /* for each module.. */
  123.     $init_membernv(Pred/Arity,Pred_name_list), 
  124.         /* is needed pred in this one? */
  125.     !,                /* yes! */
  126.     $load_mod(Module_name),        /* go load it if necessary */
  127.     $name(Module_name,Mod_name_chars),
  128.     $append(Mod_name_chars,"_export",Exp_name_chars),
  129.     $name(Exp_name,Exp_name_chars),
  130.     $bldstr(Exp_name,1,Modcall),arg(1,Modcall,Explist),
  131.     '_$call'(Modcall),
  132.     $load_preds(Explist,Pred_name_list,Module_name).
  133.  
  134. '_$definable'(Term) :-    /* no module to define pred */
  135.     $functor0(Term,Pred),
  136.     $load(Pred,0),!,        /* file exists */
  137.     (not($pred_undefined(Term))    /* file defines pred */
  138.      ;
  139.      $arity(Term,Arity),
  140.      $telling(Fi),$tell(user),
  141.      $writename(Pred),$writename('/'),$writename(Arity),
  142.      $writename(' not defined by file'),$nl,$tell(Fi),fail
  143.     ).
  144.  
  145. '_$definable'(Term) :- 
  146.     '_$nofile_msg'(1),    /* if message is to be displayed */
  147.     $telling(Fi),$tell(user),
  148.     $writename('No file to define '),
  149.     $functor0(Term,Pred),$arity(Term,Arity),
  150.     $writename(Pred),$writename('/'),$writename(Arity),$nl,$tell(Fi),fail.
  151.  
  152. $load_mod(Module_name) :-
  153.     loaded_mods(Module_name),!.    /* already loaded, noop */
  154. $load_mod(Module_name) :-
  155. /*       $telling(Fi),$tell(user),$writename('load module: '),
  156.       $writename(Module_name),$nl,$tell(Fi), */
  157.     $load(Module_name),!,         /* now loaded */
  158.     $assert(loaded_mods(Module_name)),
  159.     $load_sub_mods(Module_name).
  160. $load_mod(Module_name) :-        /* no such file */
  161.     $telling(Fi),$tell(user),
  162.     $writename('No file to define module '),
  163.     $writename(Module_name),$nl,$tell(Fi),fail.
  164.  
  165. $load_sub_mods(Module_name) :-
  166.     $name(Module_name,Mod_name_chars),
  167.     $append(Mod_name_chars,"_use",Use_name_chars),
  168.     $name(Use_name,Use_name_chars),
  169.     $bldstr(Use_name,2,Modusecall),
  170.     not($pred_undefined(Modusecall)),    /* must be defined */
  171.     /* if everything needed is already noted, no need to add them */
  172.     arg(1,Modusecall,M),arg(2,Modusecall,U),
  173.     '_$call'(Modusecall),not(defined_mods(M,U)),!,
  174.     $assert_union(defined_mods(_,_),Modusecall). /* This may result
  175.         in duplicates, but for now, that seems ok. It saves space
  176.         because we don't have to copy. It can make the search
  177.         slower. */
  178.  
  179. $load_sub_mods(_).
  180.  
  181. $load_preds([],[],_) :- !.
  182. $load_preds([],[_|_],Mod) :- !,
  183.     $telling(Fi),$tell(user),
  184.     $writename('Illegal use list for module: '),
  185.     $writename(Mod),$nl,$tell(Fi),fail.
  186. $load_preds([_|_],[],Mod) :- !,
  187.     $telling(Fi),$tell(user),
  188.     $writename('Illegal use list for module: '),
  189.     $writename(Mod),$nl,$tell(Fi),fail.
  190. $load_preds([Inname|Explist],[Inname|Pred_name_list],Mod) :- !,
  191.     $load_preds(Explist,Pred_name_list,Mod).
  192. $load_preds([Inname/Arity|Explist],[Outname/Arity|Pred_name_list],
  193.         Mod) :- !,
  194.     $bldstr(Outname,Arity,Outstr),$bldstr(Inname,Arity,Instr),
  195.     ($pred_undefined(Outstr),!,$assert_union(Outstr,Instr),
  196.         $load_preds(Explist,Pred_name_list,Mod)
  197.     ;
  198.         $telling(Fi),$tell(user),
  199.         $writename('Attempt to redefine '),$writename(Outname),
  200.         $writename('/'),$writename(Arity),$nl,
  201.         $tell(Fi),
  202.         $load_preds(Explist,Pred_name_list,Mod)
  203.     ).
  204. $load_preds([Inname/Inarity|Explist],[Outname/Outarity|Pred_name_list],
  205.         Mod) :-
  206.     $telling(Fi),$tell(user),
  207.     $writename('Incorrect import arity: '),$writename(Outname),
  208.     $writename('/'),$writename(Outarity),$nl,
  209.     $tell(Fi),
  210.     $load_preds(Explist,Pred_name_list,Mod),
  211.     fail. /* load rest, but fail */
  212.  
  213.  
  214. $load(File) :- $load(File,0).
  215. $load(File,Rc) :- '_$builtin'(11).
  216.  
  217. $init_membernv(Pa,[Tpa|_]) :- nonvar(Tpa),Pa=Tpa.
  218. $init_membernv(Pa,Pn_list) :- nonvar(Pn_list),
  219.     Pn_list=[_|Pn_tail],$init_membernv(Pa,Pn_tail).
  220.  
  221. /* the keyboard interrupt handler */
  222.  
  223. '_$keyboard_int'(Call) :- break,'_$call'(Call).
  224.  
  225. /* the general interrupt handler! */
  226.  
  227. '_$interrupt'(Call,Code) :-
  228.     Code =:= 0 -> '_$undefined_pred'(Call);
  229.       (Code =:= 1 -> '_$keyboard_int'(Call);
  230.           (Code =:= 2 -> $writename('Stack Overflow!!'),$nl,abort;
  231.         $writename('Illegal interrupt code'),halt
  232.           )
  233.       ).
  234.